package com.buba.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.feign.staffFeign;
import com.buba.mapper.InsuranceFileMapper;
import com.buba.mapper.InsuranceMapper;
import com.buba.mapper.SocialMapper;
import com.buba.mapper.SocialinformationMapper;
import com.buba.service.SocialService;
import common.R;
import entity.salary.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.salary.dto.InsuranceDto;
import entity.salary.dto.InsurcanDto;
import entity.salary.dto.accumulationDto;
import entity.salary.dto.socialDto;
import entity.salary.vo.socialDetailedVo;
import entity.salary.vo.socialVo;
import entity.staff.Accounting;
import entity.staff.Department;
import entity.staff.Post;
import entity.staff.dto.saveAccountDto;
import entity.staff.dto.updateAccSecurityDto;
import entity.staff.dto.withSocialPerDto;
import entity.staff.vo.accountingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.PageResult;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.coyote.http11.Constants.a;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@Service
public class SocialServiceImpl extends ServiceImpl<SocialMapper, Social> implements SocialService {

    @Resource
    private SocialMapper socialMapper;
    @Resource
    private staffFeign staffFeign;
    @Resource
    private SocialinformationMapper socialinformationMapper;
    @Resource
    private InsuranceMapper insuranceMapper;
    @Resource
    private InsuranceFileMapper insuranceFileMapper;
    /* *
     * @Author: beisheng
     * @Descriprion: 分页查询社保方案
     * @Date: 2023/3/15 19:00
     * @Return: common.R
     * @Param: []
     */

    @Override
    public PageResult findAllSocial(socialDto socialDto) {
        List<socialVo> socialVos = new ArrayList<>();
        //分页查询社保方案信息
        Page<Social> socialPage = Page.of(socialDto.getPage(),socialDto.getPagesize());
        Page<Social> socials = socialMapper.selectPage(socialPage, new QueryWrapper<Social>().eq("delete_flag", 0));
        List<Social> socialList = socialMapper.selectList(new QueryWrapper<Social>().eq("delete_flag", 0));
        for (Social social : socials.getRecords()) {
            String paymentItem = "";
            socialVo socialvo  = new socialVo();
            BeanUtils.copyProperties(social,socialvo);
            //查询社保公积金方案配置信息
            List<Socialinformation> socialinformations = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("social_security", 1).eq("socia_id", social.getSaId()).eq("payl_name","公司").eq("delete_flag",0));
            for (int i = 0; i < socialinformations.size(); i++) {
                //根据社保方案中的险种ID查询保险名称
                Insurance insurance = insuranceMapper.selectById(socialinformations.get(i).getSociaName());
                if(i==0){
                    paymentItem+=insurance.getInsuranceName();
                }else {
                    paymentItem+=","+insurance.getInsuranceName();
                }
            }
            if(social.getRelatedPerson()!=null){
                List<String> splitLiss = Arrays.asList(social.getRelatedPerson().split(","));
                //调用远程员工服务根据用户id查询所有用户
                List<Accounting> accountings = staffFeign.selstaffByIds(splitLiss);
                socialvo.setUsePeople(String.valueOf(accountings.size()));
                if(paymentItem.length()>4){
                    paymentItem = paymentItem.substring(0,paymentItem.length());
                }
                socialvo.setPaymentItem(paymentItem);
            }else {
                if(paymentItem.length()<=4) {
                    socialvo.setPaymentItem("暂无");
                }
                socialvo.setUsePeople("0");
            }
            socialVos.add(socialvo);
        }
        return new PageResult(socialDto.getPage(),socialDto.getPagesize(),socialList.size(),socialVos);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据社保方案ID查询当前社保方案关联的人员信息
     * @Date: 2023/3/16 10:47
     * @Return: common.R
     * @Param: [socialPerDto]
     */

    @Override
    public PageResult withSocialPerDto(withSocialPerDto socialPerDto) {
        List<accountingVo> accountingVos = new ArrayList<>();
        socialVo socialVo = new socialVo();
        String departmentName = socialPerDto.getDepartmentName();
        if(departmentName!=null){
            Department department = staffFeign.selDepartmentidByName(departmentName);
            socialPerDto.setDepartmentId(department.getOzId());
        }
        //1.根据当前方案ID查询所有把绑定当前社保公积金方案用户
        PageResult pageResult = staffFeign.selstaffBySocial(socialPerDto);
        List<Accounting> items =  new ArrayList<>();
        //判断是否有绑定当前社保人员
        if(pageResult.getItems().size()!=0){
            for (Object item : pageResult.getItems()) {
                Accounting accounting = JSON.parseObject(JSON.toJSONString(item),Accounting.class);
                items.add(accounting);
            }
            for (Accounting item : items) {
                accountingVo accountingVo  = new accountingVo();
                BeanUtils.copyProperties(item,accountingVo);
                //2.查询部门岗位信息
                Department department = staffFeign.selNameById(item.getDepartmentId());
                Post post = staffFeign.selPostNameById(item.getPostId());
                accountingVo.setDepartmentName(department.getDepartName());
                accountingVo.setPositionName(post.getPostName());
                //3.查询当前方案名称
                Social social = socialMapper.selectById(socialPerDto.getSocialId());
                accountingVo.setSchemeName(social.getSchemeName());
                //4.查询申报基数
                List<Socialinformation> socialinformations = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delete_flag", 0).eq("socia_id", socialPerDto.getSocialId()).eq("social_security",1));
                if(socialinformations.size()>0){
                    if(socialinformations.get(0).getPayCardinal()!=null){
                        accountingVo.setDeclaration(socialinformations.get(0).getPayCardinal().toString());
                    }
                }

                accountingVos.add(accountingVo);
                socialVo.setAccounting(accountingVos);
            }
        }
        //5.返回数据
        return new PageResult(socialPerDto.getSocialDto().getPage(),socialPerDto.getSocialDto().getPagesize(),pageResult.getCounts(),socialVo.getAccounting());
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 判断方案是否存在
     * @Date: 2023/3/16 15:29
     * @Return: common.R
     * @Param: [schemeName]
     */

    @Override
    public boolean judgeScheme(String schemeName) {
        Social social = socialMapper.selectOne(new QueryWrapper<Social>().eq("delete_flag",0).eq("scheme_name", schemeName));
            if(social!=null){
            return false;
        }else {
            return true;
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加社保方案
     * @Date: 2023/3/16 18:31
     * @Return: common.R
     * @Param: [insuranceDto]
     */

    @Override
    public void insertSocial(InsuranceDto insuranceDto) {
        //1.添加社保方案
        Social social = new Social();
        BeanUtils.copyProperties(insuranceDto,social);
        social.setCreateTime(new Date());
        social.setUpdateTime(new Date());
        social.setStatus(true);
        social.setDeleteFlag(0);
        socialMapper.insert(social);
        //2.添加社保方案配置信息
        Map<String, InsurcanDto> insurcanceListDtos = insuranceDto.getInsurcanceListDtos();
        Set<Map.Entry<String, InsurcanDto>> entries = insurcanceListDtos.entrySet();
        for (Map.Entry<String, InsurcanDto> entry : entries) {
            InsurcanDto value = entry.getValue();
            Socialinformation corporationinformations = value.getCorporationinformations();

            Socialinformation myinformations = value.getMyinformations();
            corporationinformations.setSocialSecurity(1);
            corporationinformations.setSociaId(social.getSaId());
            corporationinformations.setDeleteFlag(0);
            corporationinformations.setCreateTime(new Date());
            corporationinformations.setUpdateTime(new Date());
            socialinformationMapper.insert(corporationinformations);

            myinformations.setSocialSecurity(1);
            myinformations.setSociaId(social.getSaId());
            myinformations.setDeleteFlag(0);
            myinformations.setCreateTime(new Date());
            myinformations.setUpdateTime(new Date());
            socialinformationMapper.insert(myinformations);
        }
        //3.添加关联人员
        if(insuranceDto.getRelatedPerson()!=null){
            saveAccountDto saveAccountDto = new saveAccountDto();
            saveAccountDto.setSocialId(social.getSaId());
            String relatedPerson = insuranceDto.getRelatedPerson();
            List<String> list = Arrays.asList(relatedPerson.split(","));
            List<Integer> integerList = new ArrayList<>();
            for (String s : list) {
                integerList.add(Integer.valueOf(s));
            }
            saveAccountDto.setSocialIds(integerList);
            staffFeign.saveAccountToSocial(saveAccountDto);
        }
        if(insuranceDto.getRelatedPerson()!=null || insuranceDto.getInsurcanceListDtos().size()!=0){
            //查询当前社保关联的人员是否绑定参保档案
            List<Accounting> accounting = staffFeign.selInsurancefile(social.getSaId());
            for (Accounting accountings : accounting) {
                if (accountings.getInsurancefileId() != 0) {
                    InsuranceFile insuranceFile = new InsuranceFile();
                    insuranceFile.setSocialId(social.getSaId());
                    insuranceFile.setSocialStart(new Date());
                    Map<String, InsurcanDto> insurcanceListDtos1 = insuranceDto.getInsurcanceListDtos();
                    Set<Map.Entry<String, InsurcanDto>> entries1 = insurcanceListDtos1.entrySet();
                    Double a = 0.0;
                    for (Map.Entry<String, InsurcanDto> stringInsurcanDtoEntry : entries1) {
                        InsurcanDto value = stringInsurcanDtoEntry.getValue();
                        a = value.getCorporationinformations().getPayCardinal();
                    }
                    insuranceFile.setSocialCardinal(a);
                    insuranceFile.setSocialAccounts(accountings.getSocialAccount());
                    insuranceFile.setSocialRemark("社保信息");
                    insuranceFileMapper.update(insuranceFile, new QueryWrapper<InsuranceFile>().eq("sa_id", accountings.getInsurancefileId()));

                } else {
                    InsuranceFile insuranceFile = new InsuranceFile();
                    insuranceFile.setSocialId(social.getSaId());
                    insuranceFile.setSocialStart(new Date());
                    Map<String, InsurcanDto> insurcanceListDtos1 = insuranceDto.getInsurcanceListDtos();
                    Set<Map.Entry<String, InsurcanDto>> entries1 = insurcanceListDtos1.entrySet();
                    Double a = 0.0;
                    for (Map.Entry<String, InsurcanDto> stringInsurcanDtoEntry : entries1) {
                        InsurcanDto value = stringInsurcanDtoEntry.getValue();
                        a = value.getCorporationinformations().getPayCardinal();
                    }
                    insuranceFile.setSocialCardinal(a);
                    insuranceFile.setSocialAccounts(accountings.getSocialAccount());
                    insuranceFile.setSocialRemark("社保信息");
                    insuranceFileMapper.insert(insuranceFile);
                    Map<String, Integer> map = new HashMap<>();
                    map.put("socialId", insuranceFile.getSaId());
                    map.put("ozId", accountings.getOzId());
                    staffFeign.updateInsuranceFileId(map);
                }
            }
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加人员到社保方案
     * @Date: 2023/3/17 9:52
     * @Return: common.R
     * @Param: [saveAccountDto]
     */

    @Override
    public void saveAccuntToSocial(saveAccountDto saveAccountDto) {
        String socialIds = "";
        Social social = new Social();
        for (Integer socialId : saveAccountDto.getSocialIds()) {
            if(saveAccountDto.getSocialIds().size()==1){
                socialIds += socialId;
            }else {
                socialIds+=socialId+",";
            }
        }
        if(socialIds.length()>1){
            socialIds = socialIds.substring(0,socialIds.length()-1);
        }

        social.setRelatedPerson(socialIds);
        social.setSaId(saveAccountDto.getSocialId());
        socialMapper.updateById(social);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 删除方案,并删除方案配置信息
     * @Date: 2023/3/17 10:26
     * @Return: common.R
     * @Param: [id]
     */

    @Override
    public void removeSocial(Integer id) {
        Social social = new Social();
        social.setDeleteFlag(1);
        social.setSaId(id);
        Socialinformation socialinformation = new Socialinformation();
        socialinformation.setDeleteFlag(1);
        socialMapper.updateById(social);
        List<String> list = new ArrayList<>();
        list.add(id.toString());
        staffFeign.updateACCScheme(list);
        socialinformationMapper.update(socialinformation,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("socia_id",id));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量删除方案,并删除关联方案信息
     * @Date: 2023/3/17 10:29
     * @Return: common.R
     * @Param: [ids]
     */

    @Override
    public R removeSocialByIds(List<Integer> ids) {
        List<String> list = new ArrayList<>();
        List<Social> socials = socialMapper.selectBatchIds(ids);
        for (Social social : socials) {
            if(social.getStatus() == true || social.getRelatedPerson()!=null){
                return R.error("500","当前选中方案正在使用或者已经绑定人员,无法删除，请重新选择");
            }
        }
        for (Integer id : ids) {
            Social socialOne = socialMapper.selectById(id);
                Social social = new Social();
                social.setDeleteFlag(1);
                social.setSaId(id);
                Socialinformation socialinformation = new Socialinformation();
                socialinformation.setDeleteFlag(1);
                socialMapper.updateById(social);
                socialinformationMapper.update(socialinformation,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("socia_id",id).eq("delete_flag",0));
                list.add(id.toString());
        }
        if(list.size()>0){
            staffFeign.updateACCScheme(list);
        }
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 更改社保方案状态
     * @Date: 2023/3/17 10:43
     * @Return: common.R
     * @Param: [id]
     */

    @Override
    public void updateSocialStatus(Map<String, Object> map) {
        Integer socialId = (Integer) map.get("socialId");
        Boolean status = (Boolean) map.get("status");
        Social social = new Social();
        social.setSaId(socialId);
        social.setStatus(status);
        socialMapper.updateById(social);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改社保方案及社保方案配置信息及关联人员
     * @Date: 2023/3/17 11:21
     * @Return: common.R
     * @Param: [insuranceDto]
     */

    @Override
    public void updateSocial(InsuranceDto insuranceDto) {
        Social social = new Social();
        BeanUtils.copyProperties(insuranceDto,social);
        social.setUpdateTime(new Date());
        //获取配置方案信息
        Map<String, InsurcanDto> insurcanceListDtos = insuranceDto.getInsurcanceListDtos();
        Set<Map.Entry<String, InsurcanDto>> entries = insurcanceListDtos.entrySet();
        //更改关联人员
        Social socialOne = socialMapper.selectById(insuranceDto.getSocialID());
        String relatedPerson = insuranceDto.getRelatedPerson();
        if(socialOne.getRelatedPerson() == null || !socialOne.getRelatedPerson().equals(relatedPerson)){
            List<String> list = Arrays.asList(relatedPerson.split(","));
            updateAccSecurityDto updateAccSecurityDto = new updateAccSecurityDto();
            updateAccSecurityDto.setIds(list);
            updateAccSecurityDto.setSecurityId(insuranceDto.getSocialID());
            List<Accounting> accountings = staffFeign.selInsurancefile(insuranceDto.getSocialID());
            List<String> list1 = Arrays.asList(relatedPerson);
            for (Accounting accounting : accountings) {
                int count = 0;
                for (String s : list1) {
                    if(accounting.getOzId().toString().equals(s)){
                        count++;
                    }
                    if(count==0){
                        UpdateWrapper<InsuranceFile> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.set("social_id",null);
                        updateWrapper.set("social_start",null);
                        updateWrapper.set("social_cardinal",null);
                        updateWrapper.set("social_accounts",null);
                        updateWrapper.set("social_remark",null);
                        updateWrapper.eq("sa_id",accounting.getInsurancefileId());
                        insuranceFileMapper.update(null,updateWrapper);
                    }
                }
            }
            staffFeign.updateAccSecurity(updateAccSecurityDto);
        }
        //更改方案基本信息
        socialMapper.update(social,new QueryWrapper<Social>().eq("sa_id",insuranceDto.getSocialID()).eq("delete_flag",0));
        for (Map.Entry<String, InsurcanDto> entry : entries) {
            InsurcanDto value = entry.getValue();
            Socialinformation corporationinformations = value.getCorporationinformations();
            Socialinformation myinformations = value.getMyinformations();
            //判断当前社保方案配置信息有没有当前社保方案配置信息
            Socialinformation socialinformation = socialinformationMapper.selectOne(new QueryWrapper<Socialinformation>().eq("social_security",1).eq("delete_flag", 0).eq("socia_id", insuranceDto.getSocialID()).eq("socia_name", myinformations.getSociaName()).eq("payl_name","个人"));
            //如果没有当前方案,那么就添加
            if(socialinformation==null){
                corporationinformations.setSocialSecurity(1);
                corporationinformations.setSociaId(insuranceDto.getSocialID());
                corporationinformations.setDeleteFlag(0);
                corporationinformations.setCreateTime(new Date());
                corporationinformations.setUpdateTime(new Date());
                socialinformationMapper.insert(corporationinformations);

                myinformations.setSocialSecurity(1);
                myinformations.setSociaId(insuranceDto.getSocialID());
                myinformations.setDeleteFlag(0);
                myinformations.setCreateTime(new Date());
                myinformations.setUpdateTime(new Date());
                socialinformationMapper.insert(myinformations);
            //如果有当前方案,那么就修改
            }else {
                Socialinformation corporation = new Socialinformation();
                Socialinformation myin = new Socialinformation();
                BeanUtils.copyProperties(corporationinformations,corporation);
                BeanUtils.copyProperties(myinformations,myin);
                socialinformationMapper.update(corporation,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("socia_id",insuranceDto.getSocialID()).eq("sa_id",corporation.getSaId()).eq("delete_flag",0));
                socialinformationMapper.update(myin,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("socia_id",insuranceDto.getSocialID()).eq("sa_id",myin.getSaId()).eq("delete_flag",0));
            }
        }
        //判断当前方案和传递过来的方案是否一致
        List<Socialinformation> socialinformations = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("social_security",1).eq("delete_flag", 0).eq("socia_id", insuranceDto.getSocialID()));
        if(socialinformations.size()!=0){
            List<String> list = new ArrayList<>();
            for (Socialinformation socialinfor : socialinformations) {
                if(socialinfor.getPaylName().equals("公司")){
                    list.add(socialinfor.getSociaName().toString());
                }
                //如果传递过来的方案为空，那么就把数据库的已有方案逻辑删除
                if(insuranceDto.getInsurcanceListDtos().size() == 0){
                    Socialinformation socialinformation = new Socialinformation();
                    socialinformation.setDeleteFlag(1);
                    socialinformationMapper.update(socialinformation,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("delete_flag",0).eq("sa_id",socialinfor.getSaId()));
                }
            }
            if(insuranceDto.getInsurcanceListDtos().size()!=0) {
                List<String> stringList = new ArrayList<>();
                Set<Map.Entry<String, InsurcanDto>> entries1 = insuranceDto.getInsurcanceListDtos().entrySet();
                for (Map.Entry<String, InsurcanDto> stringInsurcanDtoEntry : entries1) {
                    InsurcanDto value = stringInsurcanDtoEntry.getValue();
                    stringList.add(value.getCorporationinformations().getSociaName().toString());
                }
                for (String s : list) {
                    Integer count = 0;
                    for (String s1 : stringList) {
                        if(s.equals(s1)){
                            count++;
                        }
                    }
                    if (count==0){
                        Socialinformation socialinformation = new Socialinformation();
                        socialinformation.setUpdateTime(new Date());
                        socialinformation.setDeleteFlag(1);
                        socialinformationMapper.update(socialinformation,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("socia_name",s).eq("payl_name","公司").eq("delete_flag",0));
                        Socialinformation socialinformation1 = new Socialinformation();
                        socialinformation1.setUpdateTime(new Date());
                        socialinformation1.setDeleteFlag(1);
                        socialinformationMapper.update(socialinformation1,new QueryWrapper<Socialinformation>().eq("social_security",1).eq("socia_name",s).eq("payl_name","个人").eq("delete_flag",0));
                    }
                }
            }
        }
        if(insuranceDto.getRelatedPerson()!=null) {
            //查询当前社保关联的人员是否绑定参保档案
            List<Accounting> accounting = staffFeign.selInsurancefile(insuranceDto.getSocialID());
            for (Accounting accountings : accounting) {
                if (accountings.getInsurancefileId() != 0) {
                    InsuranceFile insuranceFile = new InsuranceFile();
                    insuranceFile.setSocialId(insuranceDto.getSocialID());
                    insuranceFile.setSocialStart(new Date());
                    Map<String, InsurcanDto> insurcanceListDtos1 = insuranceDto.getInsurcanceListDtos();
                    Double a = 0.0;
                    if(insurcanceListDtos1.size()!=0){
                        Set<Map.Entry<String, InsurcanDto>> entries1 = insurcanceListDtos1.entrySet();
                        for (Map.Entry<String, InsurcanDto> stringInsurcanDtoEntry : entries1) {
                            InsurcanDto value = stringInsurcanDtoEntry.getValue();
                            a = value.getCorporationinformations().getPayCardinal();
                        }
                        insuranceFile.setSocialCardinal(a);
                    }else {
                        a=8000.00;
                    }
                    insuranceFile.setSocialCardinal(a);
                    insuranceFile.setSocialAccounts(accountings.getSocialAccount());
                    insuranceFile.setSocialRemark("社保信息");
                    insuranceFileMapper.update(insuranceFile, new QueryWrapper<InsuranceFile>().eq("sa_id", accountings.getInsurancefileId()));
                } else {
                    InsuranceFile insuranceFile = new InsuranceFile();
                    insuranceFile.setSocialId(insuranceDto.getSocialID());
                    insuranceFile.setSocialStart(new Date());
                    Map<String, InsurcanDto> insurcanceListDtos1 = insuranceDto.getInsurcanceListDtos();
                    Double a = 0.0;
                    if(insurcanceListDtos1.size()!=0){
                        Set<Map.Entry<String, InsurcanDto>> entries1 = insurcanceListDtos1.entrySet();
                        for (Map.Entry<String, InsurcanDto> stringInsurcanDtoEntry : entries1) {
                            InsurcanDto value = stringInsurcanDtoEntry.getValue();
                            a = value.getCorporationinformations().getPayCardinal();
                        }
                        insuranceFile.setSocialCardinal(a);
                    }else {
                        a=8000.00;
                    }

                    insuranceFile.setSocialAccounts(accountings.getSocialAccount());
                    insuranceFile.setSocialRemark("社保信息");
                    insuranceFileMapper.insert(insuranceFile);
                    Map<String, Integer> map = new HashMap<>();
                    map.put("socialId", insuranceFile.getSaId());
                    map.put("ozId", accountings.getOzId());
                    staffFeign.updateInsuranceFileId(map);
                }
            }
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据选择的社保方案查询缴费明细
     * @Date: 2023/3/20 19:55
     * @Return: common.R
     * @Param: [socilaId]
     */

    @Override
    public List<socialDetailedVo> socialDetailed(Integer socilaId) {
        List<socialDetailedVo> socialDetailedVos = new ArrayList<>();
        List<Socialinformation> socialinformations = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delete_flag", 0).eq("socia_id", socilaId).eq("social_security",1));
        List<Socialinformation> socialinformationList = socialinformations.stream().filter(i -> {
            return i.getPaylName().equals("公司");
        }).collect(Collectors.toList());
        List<Socialinformation> socialinformationList1 = socialinformations.stream().filter(i -> {
            return i.getPaylName().equals("公司");
        }).collect(Collectors.toList());
        List<Integer> list = new ArrayList<>();
        for (Socialinformation socialinformation : socialinformationList) {
            list.add(socialinformation.getSociaName());
        }
        List<Insurance> insuranceList = insuranceMapper.selectList(new QueryWrapper<Insurance>().eq("delete_flag", 0).eq("insurance_type",1).eq("insurance_status", 0).in("sa_id", list));
        socialDetailedVo socialDetailedVo = new socialDetailedVo();
        for (int i = 0; i < insuranceList.size(); i++) {
            socialDetailedVo.setSocialName(insuranceList.get(i).getInsuranceName());
            socialDetailedVos.add(socialDetailedVo);
        }
        for (int i = 0; i < socialinformationList.size(); i++) {
            socialDetailedVos.get(i).setSocialcardinal(socialinformations.get(i).getPayCardinal());
            socialDetailedVos.get(i).setEnterprise(socialinformations.get(i).getPayScale());
            String substring = socialinformations.get(i).getPayScale().substring(0, socialinformations.get(i).getPayScale().length() - 1);
            Double i1 =Double.valueOf(substring);
            Double a = 100.00;
            Double i2 = i1 / a;
            double v = socialinformations.get(i).getPayCardinal() * i2;
            socialDetailedVos.get(i).setEnterpriseMoney(v);
        }
        for (int i = 0; i < socialinformationList1.size(); i++) {
            socialDetailedVos.get(i).setSocialcardinal(socialinformationList1.get(i).getPayCardinal());
            socialDetailedVos.get(i).setIndividual(socialinformationList1.get(i).getPayScale());
            String substring = socialinformationList1.get(i).getPayScale().substring(0, socialinformationList1.get(i).getPayScale().length() - 1);
            Double i1 =Double.valueOf(substring);
            Double a = 100.00;
            Double i2 = i1 / a;
            double v = socialinformationList1.get(i).getPayCardinal() * i2;
            socialDetailedVos.get(i).setIndividualMoney(v);
        }
        for (entity.salary.vo.socialDetailedVo detailedVo : socialDetailedVos) {
            detailedVo.setSum(detailedVo.getEnterpriseMoney()+detailedVo.getIndividualMoney());
        }
        return socialDetailedVos;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据社保ID查询关联人员和关联险种信息
     * @Date: 2023/4/6 10:47
     * @Return: common.R
     * @Param: [socialId]
     */

    @Override
    public accumulationDto selSocialAndPeopleAndSocialFile(Integer socialId) {
        accumulationDto accumulationDto = new accumulationDto();
        //1.查询社保方案基本信息
        Social social = socialMapper.selectById(socialId);
        BeanUtils.copyProperties(social,accumulationDto);
        //2.查询方案配置信息
        List<Socialinformation> socialinformationList = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delete_flag", 0).eq("social_security",1).eq("socia_id", socialId));
        Map<String,InsurcanDto> map = new HashMap<>();
        Map<String,Socialinformation> maps = new HashMap<>();
        for (Socialinformation socialinformation : socialinformationList) {

            if(socialinformation.getSociaName() == 1){
                InsurcanDto insurcanDto = new InsurcanDto();
                if(socialinformation.getPaylName().equals("公司")){
                    Socialinformation corporationinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,corporationinformations);
                    maps.put("endowmentInsuranceCor",corporationinformations);
                }else {
                    Socialinformation myinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,myinformations);
                    maps.put("endowmentInsuranceMy",myinformations);
                }

                if(maps.get("endowmentInsuranceCor")!=null && maps.get("endowmentInsuranceMy")!=null){
                    insurcanDto.setCorporationinformations(maps.get("endowmentInsuranceCor"));
                    insurcanDto.setMyinformations(maps.get("endowmentInsuranceMy"));
                    map.put("endowmentInsurance",insurcanDto);
                }
            }

            if(socialinformation.getSociaName() == 3){
                InsurcanDto insurcanDto = new InsurcanDto();
                if(socialinformation.getPaylName().equals("公司")){
                    Socialinformation corporationinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,corporationinformations);
                    maps.put("endowmentInsuranceCor",corporationinformations);
                }else {
                    Socialinformation myinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,myinformations);
                    maps.put("endowmentInsuranceMy",myinformations);
                }

                if(maps.get("endowmentInsuranceCor")!=null && maps.get("endowmentInsuranceMy")!=null){
                    insurcanDto.setCorporationinformations(maps.get("endowmentInsuranceCor"));
                    insurcanDto.setMyinformations(maps.get("endowmentInsuranceMy"));
                    map.put("unemploymentInsurance",insurcanDto);
                }
            }

            if(socialinformation.getSociaName() == 4){
                InsurcanDto insurcanDto = new InsurcanDto();
                if(socialinformation.getPaylName().equals("公司")){
                    Socialinformation corporationinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,corporationinformations);
                    maps.put("endowmentInsuranceCor",corporationinformations);
                }else {
                    Socialinformation myinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,myinformations);
                    maps.put("endowmentInsuranceMy",myinformations);
                }

                if(maps.get("endowmentInsuranceCor")!=null && maps.get("endowmentInsuranceMy")!=null){
                    insurcanDto.setCorporationinformations(maps.get("endowmentInsuranceCor"));
                    insurcanDto.setMyinformations(maps.get("endowmentInsuranceMy"));
                    map.put("employmentInsurance",insurcanDto);
                }
            }


            if(socialinformation.getSociaName() == 5){
                InsurcanDto insurcanDto = new InsurcanDto();
                if(socialinformation.getPaylName().equals("公司")){
                    Socialinformation corporationinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,corporationinformations);
                    maps.put("endowmentInsuranceCor",corporationinformations);
                }else {
                    Socialinformation myinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,myinformations);
                    maps.put("endowmentInsuranceMy",myinformations);
                }

                if(maps.get("endowmentInsuranceCor")!=null && maps.get("endowmentInsuranceMy")!=null){
                    insurcanDto.setCorporationinformations(maps.get("endowmentInsuranceCor"));
                    insurcanDto.setMyinformations(maps.get("endowmentInsuranceMy"));
                    map.put("maternityInsurance",insurcanDto);
                }
            }

            if(socialinformation.getSociaName() == 6){
                InsurcanDto insurcanDto = new InsurcanDto();
                if(socialinformation.getPaylName().equals("公司")){
                    Socialinformation corporationinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,corporationinformations);
                    maps.put("endowmentInsuranceCor",corporationinformations);
                }else {
                    Socialinformation myinformations  = new Socialinformation();
                    BeanUtils.copyProperties(socialinformation,myinformations);
                    maps.put("endowmentInsuranceMy",myinformations);
                }

                if(maps.get("endowmentInsuranceCor")!=null && maps.get("endowmentInsuranceMy")!=null){
                    insurcanDto.setCorporationinformations(maps.get("endowmentInsuranceCor"));
                    insurcanDto.setMyinformations(maps.get("endowmentInsuranceMy"));
                    map.put("medicalInsurance",insurcanDto);
                }
            }
        }
        accumulationDto.setInsurcanceListDtos(map);
        return accumulationDto;
    }
}
